package org.teremail.schema;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TableBuilder {

    private final List<ColumnBuilder> columns = new ArrayList<ColumnBuilder>();
    private final List<List<String>> uniques = new ArrayList<List<String>>();
    private String name;

    public TableBuilder(String name) {
        this.name = name;
    }

    public ColumnBuilder column(String name, DBType type) {
        ColumnBuilder cb = new ColumnBuilder(name, type);
        columns.add(cb);
        return cb;
    }

    public Table build() {
        List<Column> columns = buildColumns();
        return new Table(name, columns, uniques);
    }

    private List<Column> buildColumns() {
        Column[] cols = new Column[columns.size()];
        int i = 0;
        for (ColumnBuilder cb : columns) {
            cols[i] = cb.build();
            i++;
        }
        return Arrays.asList(cols);
    }

    public void addUnique(String...cols) {
        uniques.add(Arrays.asList(cols));
    }

}
